package xrz.cuanbao.adapters;

import java.util.ArrayList;
import java.util.List;

import org.xutils.x;
import org.xutils.image.ImageOptions;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import xrz.cuanbao.R;
import xrz.cuanbao.entitys.CuanVideo;
import xrz.cuanbao.utils.Utils;

public class VideoAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mInflater;
	private List<CuanVideo> images = new ArrayList<CuanVideo>();

	ImageOptions imageOptions = new ImageOptions.Builder().setIgnoreGif(false)
			.setImageScaleType(ImageView.ScaleType.CENTER).build();

	public VideoAdapter(Context context,  List<CuanVideo> list) {
		mContext = context;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		images.addAll(list);
	}

	@Override
	public int getCount() {
		return images.size() == 0 ? 0 : Integer.MAX_VALUE;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.image_item, null);
		}
		if (images.size() == 0)
			return convertView;
		int num = 0;
		if (position <= images.size() - 1) {
			num = position;
		} else {
			num = position % images.size();
		}
		final CuanVideo video = images.get(num);
		ImageView img_voide = (ImageView) convertView.findViewById(R.id.img_voide);
		x.image().bind(((ImageView) convertView.findViewById(R.id.imgView)), images.get(num).thumb);
		((ImageView) convertView.findViewById(R.id.img_voide)).setVisibility(View.VISIBLE);
		img_voide.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Utils.playVideo(mContext, video.video);
			}
		});
		return convertView;
	}

}
